#include <stdlib.h>
#include <stdio.h>
#include "ebook/ebook.h"        
#include "errorcode.h"
#include "converter/utf16encode.h"
#include <string.h>
#include <string>

ErrorCode validateOption(int argc, char** argv)
{
    if(argc == 5)
    {
        char* command = argv[1];
        if(strcmp(command, "-i") == 0)
        {
            return OK;
        } 
    }
    
    if(argc == 4)
    {
        char* command = argv[1];
        if(strcmp(command, "-c") == 0)
        {
            return OK;
        }
    }
    
    return PARAMETERS_ERROR;
}

void generateBackBookIndex(char** argv)
{
    const char* ebookPath = argv[2];
    const char* dicPath = argv[3];
    const char* outputPath = argv[4];
    try
    {
        EBook* book = new EBook(ebookPath, dicPath);
        book->getBackBockIndex().write(outputPath);
    }
    catch(FileException& e)
    {
        printf("%s\n", e.what());
    }    
}

void transformUtf8ToUtf16(char** argv)
{
    try
    {
        std::string ebookPath = argv[2];
        std::string outputPath = argv[3];
        Converter c;
        std::string result = c.utf8ToUtf16(ebookPath);
        c.writeText(result, outputPath);
    }
    catch(std::exception& e)
    {
        printf("%s\n", e.what());
    }
}

void executeCommand(const int& argc, char** argv)
{
    switch (argc)
    {
        case 5: generateBackBookIndex(argv);
                break;
        case 4: transformUtf8ToUtf16(argv);
                break;
    }
    
}

int main(int argc, char** argv)
{
    
    
    ErrorCode option = validateOption(argc, argv);
    
    switch(option)
    {
        case OK: executeCommand(argc, argv);
                 break;
        case PARAMETERS_ERROR: printf("Parameters error\n");
                 break;
    }
    
    printf("Successfull\n");
}
